package com.xinruke.hostels.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class CollectionUtil {
    
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Collection collection) {
        if (null == collection) {
            return true;
        } else {
            return collection.isEmpty();
        }
    }
    
    public static boolean isEmpty(Object[] objects){
        return (objects==null || objects.length==0)?true:false;
    }
    
    /**
     * 数组转换为List
     * @param arr
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static List arrayToList(Object[] arr) { 
            List list = new ArrayList(); 
            if (arr == null) return list; 
            list = Arrays.asList(arr); 
            return list; 
    }
    
    /**
     * 根据key升序排序
     * @param unSortMap 未排序的map
     * @return 排序后的map
     */
    public static Map<String, Object> mapSortByKey(Map<String, Object> unSortMap) {
        TreeMap<String, Object> result = new TreeMap<String, Object>();
        Map<String, Object> maps = new HashMap<String, Object>();
        Object[] unSortKey = unSortMap.keySet().toArray();
        Arrays.sort(unSortKey);

        for (int i = 0; i < unSortKey.length; i++) {
            result.put(unSortKey[i].toString(), unSortMap.get(unSortKey[i]));
        }
        if (!result.isEmpty()) {
            maps = result.tailMap(result.firstKey());
        } else {
            maps = result;
        }
        return maps;
    }

}
